﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Excel;
using MySql.Data.MySqlClient;
using OtechWebServer.CommClass;
using DataTable = System.Data.DataTable;
using System.Threading;


namespace OtechWebServer
{
    public partial class PostTaxiRouteTest : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
            string phoneNumber = "";           
            if (Request.QueryString["PhoneNumber"] != null &&
                Request.QueryString["StartTime"] != null &&
                Request.QueryString["EndTime"] != null &&
                Request.QueryString["Distance"] != null &&
                Request.QueryString["State"] != null)                
            {
                phoneNumber = Request.QueryString["PhoneNumber"];
                
                //int id = int.Parse(Request.QueryString["ID"]);
                //string key = phoneNumber + "|" + id;
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select tcompany.CompanyName,ttaxi.TaxiNo,");
                strSql.Append("tdriver.Name from tcompany ");
                strSql.Append("left join ttaxi on tcompany.id=ttaxi.CompanyId ");
                strSql.Append("left join tdriver on tdriver.TaxiId =ttaxi.id ");
                strSql.Append("where tdriver.PhoneNumber='");
                strSql.Append(phoneNumber);
                strSql.Append("';");                
                string company = "";
                string taxiNo = "";
                string driver = "";
                DataSet ds = MySqlHealper.Query(strSql.ToString());
                if (ds==null || ds.Tables.Count==0)
                {
                    Response.Write("-2");//数据库查询异常
                    return;
                }              
                if (ds.Tables[0].Rows.Count==0)
                {
                    Response.Write("-3");//未找到该号码所在的公司
                    return;
                }                   
                company = ds.Tables[0].Rows[0][0].ToString();
                taxiNo = ds.Tables[0].Rows[0][1].ToString();
                driver = ds.Tables[0].Rows[0][2].ToString();                

                TaxiRouteInfo modle = new TaxiRouteInfo();
                //modle.Id = id;
                modle.PhoneNumber = phoneNumber;
                modle.ServerTime = DateTime.Now;
                modle.Company = company;
                modle.TaxiNo = taxiNo;
                modle.Driver = driver;

                DateTime dtTemp = new DateTime();
                if (!DateTime.TryParse(Request.QueryString["StartTime"],out dtTemp))
                {
                    Response.Write("-4");//起始时间格式错误
                    return;
                }                
                modle.StartTime = dtTemp;
                if (!DateTime.TryParse(Request.QueryString["EndTime"], out dtTemp))
                {
                    Response.Write("-5");//结束时间格式错误
                    return;
                }                
                modle.EndTime = dtTemp;
                double doubTemp;
                if (!double.TryParse(Request.QueryString["Distance"], out doubTemp))
                {
                    Response.Write("-6");//里程格式错误
                    return;
                }                
                modle.Distance = doubTemp;
                int intTemp;
                if (!int.TryParse(Request.QueryString["State"], out intTemp))
                {
                    Response.Write("-7");//载客/空载状态格式错误
                    return;
                }                
                modle.State = intTemp; 

                if (Application[company]!=null)
                {
                    List<TaxiRouteInfo> list = Application[company] 
                        as List<TaxiRouteInfo>;
                    if (list!=null)
                    {
                        list.Add(modle);
                    }
                    if (list.Count>=3)
                    {
                        Application.Lock();
                        //插入数据库
                        ListToDatatable(company, list);
                        list.Clear();
                        Application.UnLock();

                    }
                }
                else
                {
                    List<TaxiRouteInfo> list = new List<TaxiRouteInfo>();
                    list.Add(modle);
                    Application.Add(company,list);
                }               
                Response.Write("1");//正常
            }
            else
            {
                Response.Write("-1");
            } 
        
        }

        private void ListToDatatable(string company,List<TaxiRouteInfo> list)
        {

            string path = "D:\\TaxiServer_Excel";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += "\\TaxiRouteHistory";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += "\\"+company;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += "\\" + DateTime.Today.ToString("yyyy_MM_dd");
            path += ".xls";

            ExcelClass excelClass;
            if (!File.Exists(path))
            {
                excelClass = new ExcelClass();
            }
            else
            {
                excelClass = new ExcelClass(path);
            }
            DataTable dt = new DataTable();
            dt.Columns.Add("手机号码");
            dt.Columns.Add("车牌号码");
            dt.Columns.Add("司机姓名");
            dt.Columns.Add("起始时间");
            dt.Columns.Add("结束时间");
            dt.Columns.Add("上报服务器时间");
            dt.Columns.Add("行驶里程");
            dt.Columns.Add("状态");
            foreach (TaxiRouteInfo routeInfo in list)
            {
                DataRow row = dt.NewRow();
                row["手机号码"] = routeInfo.PhoneNumber;
                row["车牌号码"] = routeInfo.TaxiNo;
                row["司机姓名"] = routeInfo.Driver;
                dt.Rows.Add(row);
            }
            excelClass.AppendData(dt);
            excelClass.Save(path);            
        }
         
    }
}